<?php


class Controller_ProjectFlow extends Controller_Website {
    
    public function action_list() {
        $m_projectFlow = Model::factory('projectFlow');
        
        $where = array();
        $where['ORDER'] = 'id ASC';
        $total = $m_projectFlow->count($where);
        $pager = new Pager($total, 20);
        $list = $m_projectFlow->select($pager->offset, $pager->size,$where)->as_array();
        
        $m_projectCategory = Model::factory('projectCategory');
        $where = array();
//         $where['parent_id'] = 1;
        $columns = 'name,id';
        $c_list = $m_projectCategory->getAll($where,$columns)->as_array();
//         var_dump($list);
        
        $this->content = View::factory('projectFlow/list');
        $this->content->list = $list;
        $this->content->c_list = $c_list;
        $this->content->pager = $pager;

    }
    
    public function action_add() {
        $m_projectFlow = Model::factory('projectFlow');
        
        if (!empty($_POST)) {
            $data = $this->_get_data($_POST);
//             var_dump($_POST);exit;
            $ret = $m_projectFlow->insert($data);
            if ($ret !== false) {
                $this->redirect('projectFlow/list');
            }
        }
        
        $m_projectCategory = Model::factory('projectCategory');
//         $where = array();
//         $where['parent_id'] = 1;
//         $columns = 'name,id';
//         $c_list = $m_projectCategory->getAll($where,$columns)->as_array();
        $c_list = $m_projectCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($c_list);
        
        $this->content = View::factory('projectFlow/add');
        $this->content->c_list = $c_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->info = null;
    }
    
    public function action_del() {
        $id = $_GET['id'];
        $m_projectFlow = Model::factory('projectFlow');
        $ret = $m_projectFlow->deleteById($id);
//         var_dump($ret);exit;
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
    protected function _get_data($post) {
        return array_intersect_key($post, array_flip(array('project_cid','before_1','before_2','middle_1','middle_2','middle_3','middle_4','after_1','after_2','after_3','after_4','after_5')));
    }
}